package juc2.synchronizeddome;

/**
 * 轻量级锁 Demo
 */

public class LightweightLockDemo {
    private static final Object lock = new Object();
    private static int count = 0;
    private static final int MAX_COUNT = 300;

    public static void main(String[] args) throws InterruptedException {
        // JVM参数：-XX:+UnlockDiagnosticVMOptions -XX:-UseBiasedLocking -XX:+PrintBiasedLockingStatistics 关闭偏向锁，打开锁升级日志
//        # total entries: 0
//        # biased lock entries: 0     //偏向锁次数
//        # anonymously biased lock entries: 0
//        # rebiased lock entries: 0
//        # revoked lock entries: 0
//        # handshakes entries: 0
//        # fast path lock entries: 1143  //轻量级锁次数
//        # slow path lock entries: 314  //重量级锁次数

        // 线程1：处理 count%3 == 0
        Thread t1 = new Thread(() -> {
            while (count < MAX_COUNT) {
                synchronized (lock) {
                    // 循环等待，直到轮到自己处理（避免无效释放后立即争抢）
                    while (count < MAX_COUNT && count % 3 != 0) {
                        try {
                            lock.wait(); // 释放锁，等待被唤醒
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (count < MAX_COUNT) {
                        count++;
                        lock.notifyAll(); // 唤醒其他线程
                    }
                }
            }
        }, "Thread-1");

        // 线程2：处理 count%3 == 1（逻辑同上）
        Thread t2 = new Thread(() -> {
            while (count < MAX_COUNT) {
                synchronized (lock) {
                    while (count < MAX_COUNT && count % 3 != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (count < MAX_COUNT) {
                        count++;
                        lock.notifyAll();
                    }
                }
            }
        }, "Thread-2");

        // 线程3：处理 count%3 == 2（逻辑同上）
        Thread t3 = new Thread(() -> {
            while (count < MAX_COUNT) {
                synchronized (lock) {
                    while (count < MAX_COUNT && count % 3 != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (count < MAX_COUNT) {
                        count++;
                        lock.notifyAll();
                    }
                }
            }
        }, "Thread-3");

        long start = System.currentTimeMillis();
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        long end = System.currentTimeMillis();

        System.out.println("优化后耗时：" + (end - start) + "ms");
    }
}


